//
//  RootViewController.m
//  2_UICollectionView
//
//  Created by 愤怒的振振 on 15/6/10.
//  Copyright (c) 2015年 LiuWeiZhen. All rights reserved.
//

#import "RootViewController.h"
#import "HeaderView.h"
#import "FooterView.h"
#import "MyCell.h"

@interface RootViewController () <UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout>
{
    UICollectionView *_collectionView;
}
@end

@implementation RootViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self setUp];
    
    // extern修饰的变量，表示这个变量在其他的外部文件中，编译器去其他文件中找这个叫kDog的东西
    extern NSString *kDog;
    NSLog(@"%@", kDog);
}

- (void)setUp {
    // Step 1> 新建布局对象
    // UICollectionViewLayout 集合视图的布局类，是一个抽象基类,一般使用它的子类实现对cell的布局
    // UICollectionViewFlowLayout : UICollectionViewLayout 系统提供的，能够实现cell的网格布局
    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
    [flowLayout setScrollDirection:UICollectionViewScrollDirectionVertical];
    // 设置行间距
    // 对于垂直滚动，上下cell的间距，对于水平滚动，左右cell的间距 最小间距
    flowLayout.minimumLineSpacing = 10;
    
    // 设置item之间间距，实际间隔需要根据item的size和edgesInsets的值来调节
    flowLayout.minimumInteritemSpacing = 0;
    
    // 全局设置itemSize
    flowLayout.itemSize = CGSizeMake(170, 120);
    
    // 内边距
    flowLayout.sectionInset = UIEdgeInsetsMake(10, 10, 10, 10);
    
    // Step 2> 新建collectionView，设置它的delegate和dataSource, 并把布局对象赋给它
    self.automaticallyAdjustsScrollViewInsets = NO;
    _collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 64, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height-64) collectionViewLayout:flowLayout];
    _collectionView.delegate = self;
    _collectionView.dataSource = self;
    
    // Step 3> 注册cell, 注册section的头视图和尾视图
    [_collectionView registerNib:[UINib nibWithNibName:@"MyCell" bundle:nil] forCellWithReuseIdentifier:@"MyCell"];
    // [_collectionView registerClass:[MyCell class] forCellWithReuseIdentifier:@"MyCell"];
    
    // -- 注册头视图 header view 对于头和尾，同样可以新建xib文件，注册xib文件 --
    [_collectionView registerClass:[HeaderView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"Header"];
    
    // -- 注册尾视图 footer view --
    [_collectionView registerClass:[FooterView class] forSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:@"Footer"];
    
    [self.view addSubview:_collectionView];
}

#pragma mark -  <UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout>
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return 20;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    MyCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"MyCell" forIndexPath:indexPath];
    /**
    if (cell == nil) {
        cell = ....
    }*/
    
    cell.imageView.image = [UIImage imageNamed:@"test.png"];
    
    cell.nameLabel.text = [NSString stringWithFormat:@"小强_%d", arc4random()%100];
    return cell;
}

// 指定每一个item的大小, 这个代理方法会覆盖layout.itemSize = CGSizeMake(170, 120)
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
    return CGSizeMake(170, 120);
}

// section的头视图尺寸
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section{
    // 如果垂直滚动 view的width与集合视图一致, 需要设置高度
    return CGSizeMake(0, 60);
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForFooterInSection:(NSInteger)section{
    return CGSizeMake(30, 30);
}

// layout.sectionInset = UIEdgeInsetsMake(10, 10, 10, 10)
// UIEdgeInsets 描述的是一个视图相对于另一个视图上、左、下、右的距离
- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section {
    return UIEdgeInsetsMake(10, 10, 10, 10);
}

// section的头和尾视图 supplementaryView
- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath {
    UICollectionReusableView *view = nil;
    if ([kind isEqualToString:UICollectionElementKindSectionHeader]) {
        view = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"Header" forIndexPath:indexPath]; // 从复用队列中取，如果没有，系统会自己建立
    } else {
         view = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:@"Footer" forIndexPath:indexPath];
    }
    return view;
}

@end
